package com.fyyg.common.util.date;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
	
	
	public static java.util.Date getDateTimefromNormalStr(String strDatetime) {
		java.util.Date ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			ret = sdf.parse(strDatetime);
		} catch (java.text.ParseException e) {
			ret = null;
		}
		return ret;
	}
	
	public static java.util.Date getDateTimefromPureNumStr(String strDatetime) {
		java.util.Date ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			ret = sdf.parse(strDatetime);
		} catch (Exception e) {
			ret = null;
		}
		return ret;
	}
	
	public static String getPureNumDateStr() {
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		ret = sdf.format(new Date());
		return ret;
	}
	
	public static String getPureNumDateStr(Date date) {
		if (null== date)
			return null;
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		ret = sdf.format(date);
		return ret;
	}
	
	public static String getPureNumDateStr(Time time) {
		if (null== time)
			return null;
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		ret = sdf.format(time);
		return ret;
	}
	
	public static String getPureNumDateTimeStr() {
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		ret = sdf.format(new Date());
		return ret;
	}
	public static String getPureNumDateTimeStrtwo() {
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		ret = sdf.format(new Date());
		return ret;
	}
	
	public static String getPureNumDateTimeStr(Date date) {
		String ret = null;
		if (null==date)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		ret = sdf.format(date);
		return ret;
	}
	
	public static String getNormalDateTimeStr(Date date) {
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ret = sdf.format(date);
		return ret;
	}
	
	public static String getSpecialTimeStr() {
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
		ret = sdf.format(new Date());
		ret = ret.substring(1);
		return ret;
	}
	
	public static String getNormalDateStr() {
		String ret = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ret = sdf.format(new Date());
		return ret;
	}
	

	/**
	 * 将timestamp转换成String
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String timestampToStr(Timestamp timestamp) {
		return DateUtil.timestampToStr(timestamp, null);
	}

	/**
	 * 将timestamp转换成String
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String timestampToStr(Timestamp timestamp, String aMask) {
		String ret = null;
		String mask = aMask;
		if (mask == null || "".equals(mask)){
			mask = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(mask);
		ret = sdf.format(timestamp);
		return ret;
	}

	/**
	 * 将date转换成String
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String dateToStr(Date date) {
		return DateUtil.dateToStr(date, null);
	}

	/**
	 * 将date转换成String
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String dateToStr(Date date, String aMask) {
		String ret = null;
		String mask = aMask;
		if (mask == null || "".equals(mask)){
			mask = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(mask);
		ret = sdf.format(date);
		return ret;
	}
	
	public static String addDateToNormalStr(Date date, int dayNum) {
		String ret = null;
		if(null == date)
			return null;
		GregorianCalendar gc=new GregorianCalendar(); 
		gc.setTime(date); 
		gc.add(Calendar.DAY_OF_MONTH,dayNum); 
		ret = getNormalDateTimeStr(gc.getTime());
		return ret;
	}
	public static Date addDate(Date date, int dayNum) {
		Date dt = null;
		if(null == date)
			return null;
		GregorianCalendar gc=new GregorianCalendar(); 
		gc.setTime(date); 
		gc.add(Calendar.DAY_OF_MONTH,dayNum); 
		dt = gc.getTime();
		return dt;
	}

	/**
	 * 将String转换成Date
	 * 
	 * @param timestamp
	 * @return
	 */
	public static java.util.Date strToDate(String date) {
		return DateUtil.strToDate(date, null);
	}

	public static String nowDateTostr(String pattern) {
		String mask = pattern;
		if (mask == null || "".equals(mask)){
			mask = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String ret = sdf.format(new Date());

		return ret;
	}

	/**
	 * 将String转换成Date
	 * 
	 * @param timestamp
	 * @return
	 */
	public static java.util.Date strToDate(String date, String aMask) {
		java.util.Date ret = null;
		String mask = aMask;
		if (mask == null || "".equals(mask)){
			mask = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(mask);
		try {
			ret = sdf.parse(date);
		} catch (java.text.ParseException e) {
			ret = null;
		}
		return ret;
	}

	public static String lpadnum(int num, int length) {
		int len = String.valueOf(num).length();
		StringBuffer str = new StringBuffer("");
		if (len < length) {
			for (int i = 0; i < length - len; i++) {
				str.append("0");
			}
			return str.append(num).toString();
		} else {
			return String.valueOf(num);
		}
	}

	/**
	 * 返回当前的GregorianCalendar
	 * 
	 * @return
	 */
	public static Calendar getToday() {
		return new GregorianCalendar();
	}

	/**
	 * 返回当前日期的年
	 * 
	 * @return
	 */
	public static String getYear() {
		Calendar calendar = DateUtil.getToday();
		return String.valueOf(calendar.get(Calendar.YEAR));
	}

	/**
	 * 返回当前日期的月
	 * 
	 * @return
	 */
	public static String getMonth() {
		Calendar calendar = DateUtil.getToday();
		int month = calendar.get(Calendar.MONTH) + 1;
		return DateUtil.lpadnum(month, 2);
	}

	/**
	 * 返回当前日期的天
	 * 
	 * @return
	 */
	public static String getDay() {
		Calendar calendar = DateUtil.getToday();
		return DateUtil.lpadnum(calendar.get(Calendar.DAY_OF_MONTH), 2);
	}

	/**
	 * 返回当前日期的小时数
	 * 
	 * @return
	 */
	public static String getHour() {
		Calendar calendar = DateUtil.getToday();
		return DateUtil.lpadnum(calendar.get(Calendar.HOUR_OF_DAY), 2);
	}

	/**
	 * 返回当前日期的分钟
	 * 
	 * @return
	 */
	public static String getMinute() {
		Calendar calendar = DateUtil.getToday();
		return DateUtil.lpadnum(calendar.get(Calendar.MINUTE), 2);
	}

	/**
	 * 返回当前日期的秒钟
	 * 
	 * @return
	 */
	public static String getSecond() {
		Calendar calendar = DateUtil.getToday();
		return DateUtil.lpadnum(calendar.get(Calendar.SECOND), 2);
	}

	/**
	 * 返回当前日期的毫秒
	 * 
	 * @return
	 */
	public static String getMilliSecond() {
		Calendar calendar = DateUtil.getToday();
		return DateUtil.lpadnum(calendar.get(Calendar.MILLISECOND), 3);
	}

	/**
	 * 返回当前日期
	 * 
	 * @return
	 */
	public static String getDate() {
		return DateUtil.getYear() + "-" + DateUtil.getMonth() + "-"
				+ DateUtil.getDay();
	}

	/**
	 * 返回带时间的日期
	 * 
	 * @return
	 */
	public static String getDateWithTime() {
		return DateUtil.getDate() + " " + DateUtil.getHour() + ":"
				+ DateUtil.getMinute() + ":" + DateUtil.getSecond();
	}

	/**
	 * 返回带时间的毫秒
	 * 
	 * @return
	 */
	public static String getDateWithMilliSecond() {
		return DateUtil.getDate() + " " + DateUtil.getHour() + ":"
				+ DateUtil.getMinute() + ":" + DateUtil.getSecond() + " "
				+ DateUtil.getMilliSecond();
	}

	/**
	 * 
	 * @param date
	 * @param mode为Calendar里面的参数
	 * @return
	 */
	public static String get(java.util.Date date, int mode) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setGregorianChange(date);
		return String.valueOf(DateUtil.lpadnum(calendar.get(mode), 2));
	}

	/**
	 * 将一个日期增加多少天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static String addDay(java.util.Date date, int day) {
		return DateUtil.add(date, day, "day");
	}

	/**
	 * 将一个日期增加多少天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static String addDay(String date, int day) {
		return DateUtil.addDay(DateUtil.strToDate(date), day);
	}

	/**
	 * 将一个日期增加多少个月
	 * 
	 * @param date
	 * @param month
	 * @return
	 */
	public static String addMonth(java.util.Date date, int month) {
		return DateUtil.add(date, month, "month");
	}

	/**
	 * 将一个日期增加多少个月
	 * 
	 * @param date
	 * @param month
	 * @return
	 */
	public static String addMonth(String date, int month) {
		return DateUtil.addMonth(DateUtil.strToDate(date), month);
	}

	/**
	 * 比较日期
	 * 
	 * @param atime
	 * @param btime
	 * @return
	 */
	public static int complareDate(java.util.Date atime, java.util.Date btime) {
		GregorianCalendar acalc = (GregorianCalendar) getToday();
		GregorianCalendar bcalc = (GregorianCalendar) getToday();
		acalc.setTime(atime);
		bcalc.setTime(btime);
		if (acalc.after(bcalc)) {// a的时间在b的时间后面 a>b
			return 1;
		} else {
			if (acalc.before(bcalc)) {// a的时间在b的时间前面 a<b
				return -1;
			} else {
				return 0;// 两个时间相等 a=b
			}
		}
	}

	/**
	 * 只比较日期,没有比较时间
	 * 
	 * @param atime
	 * @param btime
	 * @return
	 */
	public static int complareDate(String atime, String btime) {
		return complareDate(strToDate(atime), strToDate(btime));
	}

	public static String add(java.util.Date date, int num, String mode) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		if (mode.equalsIgnoreCase("month")) {
			calendar.add(Calendar.MONTH, num);
		} else {
			calendar.add(Calendar.DAY_OF_MONTH, num);
		}

		return String.valueOf(calendar.get(Calendar.YEAR)) + "-"
				+ DateUtil.lpadnum(calendar.get(Calendar.MONTH) + 1, 2) + "-"
				+ DateUtil.lpadnum(calendar.get(Calendar.DAY_OF_MONTH), 2);
	}

	/**
	 * 返回当前时间 dominic
	 * 
	 * @return 返回当前时间
	 */
	public static Date getCurrentDateTime() {
		java.util.Calendar calNow = java.util.Calendar.getInstance();
		java.util.Date dtNow = calNow.getTime();

		return dtNow;
	}

	/**
	 * 获得当前字符串时间
	 */
	public static String getCurrentTime() {
		return DateUtil.getYear() + DateUtil.getMonth() + DateUtil.getDay()
				+ DateUtil.getHour() + DateUtil.getMinute()
				+ DateUtil.getSecond();
	}

	/**
	 * 时间差 dominic
	 * 
	 * @return 返回两个时间差几秒
	 */
	public static int diffTime(Date date1, Date date2) {
		GregorianCalendar c1 = new GregorianCalendar();
		c1.setTime(date1);
		GregorianCalendar c2 = new GregorianCalendar();
		c2.setTime(date2);
		double timeLong = c2.getTimeInMillis() - c1.getTimeInMillis();
		return (int) timeLong / 1000;
	}

	/**
	 * 时间判断indate是否在startdate与enddate之间
	 * 
	 * @param indate
	 * @param startdate
	 * @param enddate
	 * @return
	 */
	public static boolean betweenTime(Date indate, Date startdate, Date enddate) {
		if (diffTime(indate, startdate) < 0 && diffTime(indate, enddate) > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 时间格式化
	 * @param date 需要格式化的时间
	 * @param pattern 日期转换格试，为null时按"yyyy-MM-dd HH:mm:ss"处理
	 * @author zdx
	 * @return date为null或空,返回空字符
	 */
	public static String formatDate(Date date,String pattern) {
		if(pattern == null || "".equals(pattern)){
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		if(date != null && !"".equals(date)){
			return format.format(date);
		}else{
			return "";
		}
	}
	
	
	public static String getFirstDayOfMonth(int year,int month)
	  {
	    Calendar cal = Calendar.getInstance();
	    //设置年份
	    cal.set(Calendar.YEAR,year);
	    //设置月份
	    cal.set(Calendar.MONTH, month-1);
	    //设置日历中月份的第1天
	    cal.set(Calendar.DAY_OF_MONTH, 1);
	    //格式化日期
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    String firstDayOfMonth = sdf.format(cal.getTime());
	    
	    return firstDayOfMonth ;
	  }
	
	
	
	public static String stringDateFormat(String timeMillis,String pattern){
		String vv = String.valueOf(timeMillis);
		long time = 0L;
		if (vv.length() < 11)
			time = Long.valueOf(vv).longValue() * 1000L;
		else {
			time = Long.valueOf(vv).longValue();
		}
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		SimpleDateFormat dateformat = new SimpleDateFormat(pattern);
		return dateformat.format(c.getTime());
	}


	
	/**
	 * 月份第一天
	 * @param month
	 * @return
	 */
	public static String LastDay(int month) {
		Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month-1);
        int maxday =calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, maxday);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(calendar.getTime());
 
    }
	/**
	 * 月份最后一天
	 * @param month
	 * @return
	 */
    public static String FirstDay(int month) {
    	 Calendar calendar = Calendar.getInstance();
    	 calendar.set(Calendar.MONTH, month-1);
         int minday =calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
         calendar.set(Calendar.DAY_OF_MONTH, minday);
         //格式化日期
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         return  sdf.format(calendar.getTime());
    }
    
    public static void main(String[] args) throws InterruptedException {
    	Date str = DateUtil.getDateTimefromNormalStr("2019-10-29");
    	System.out.println(DateUtil.strToDate("2019-10-29"));
    	
    	System.out.println(FirstDay(9));
        System.out.println(LastDay(9));
    }
	    
	    
}
